package com.youkeda.comment.service.impl;

import com.youkeda.comment.dao.UserDAO;
import com.youkeda.comment.dataobject.UserDO;
import com.youkeda.comment.model.Result;
import com.youkeda.comment.model.User;
import com.youkeda.comment.service.UserService;
import com.youkeda.comment.util.UserDOUtil;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.ibatis.annotations.Param;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@DubboService
public class UserServiceImpl implements UserService {

    @Resource
    private UserDAO userDAO;

    @PostConstruct
    public void init() {
        System.out.println("UserServiceImpl init successful.");
    }

    @Override
    public Result<User> register(String userName, String pwd) {
        Result<User> result = new Result<>();

        if (StringUtils.isEmpty(userName)) {
            result.setCode("600");
            result.setMessage("用户名不能为空");
            return result;
        }
        if (StringUtils.isEmpty(pwd)) {
            result.setCode("601");
            result.setMessage("密码不能为空");
            return result;
        }

        UserDO userDO = userDAO.findByUserName(userName);
        if (userDO != null) {
            result.setCode("602");
            result.setMessage("用户名已经存在");
            return result;
        }

        // 密码加自定义盐值，确保密码安全
        String saltPwd = pwd + "_ykd2050";
        // 生成md5值，并转大写字母
        String md5Pwd = DigestUtils.md5Hex(saltPwd).toUpperCase();

        UserDO userDO1 = new UserDO();
        userDO1.setUserName(userName);
        // 初始昵称等于用户名
        userDO1.setNickName(userName);
        userDO1.setPwd(md5Pwd);

        userDAO.add(userDO1);

        result.setSuccess(true);

        result.setData(userDO1.toModel());

        return result;
    }

    @Override
    public Result<User> login(String userName, String pwd) {

        Result<User> result = new Result<>();

        if (StringUtils.isEmpty(userName)) {
            result.setCode("600");
            result.setMessage("用户名不能为空");
            return result;
        }
        if (StringUtils.isEmpty(pwd)) {
            result.setCode("601");
            result.setMessage("密码不能为空");
            return result;
        }

        UserDO userDO = userDAO.findByUserName(userName);
        if (userDO == null) {
            result.setCode("602");
            result.setMessage("用户名不存在");
            return result;
        }

        // 密码加自定义盐值，确保密码安全
        String saltPwd = pwd + "_ykd2050";
        // 生成md5值，并转大写字母
        String md5Pwd = DigestUtils.md5Hex(saltPwd).toUpperCase();

        if (!StringUtils.equals(md5Pwd, userDO.getPwd())) {
            result.setCode("603");
            result.setMessage("密码不对");
            return result;
        }

        result.setSuccess(true);

        result.setData(userDO.toModel());

        return result;
    }

    @Override
    public int batchAdd(@Param("list") List<User> users) {
        List<UserDO> userDOs = new ArrayList<>();

        for (User u : users) {
            userDOs.add(UserDOUtil.toDO(u));
        }

        return userDAO.batchAdd(userDOs);
    }

    @Override
    public List<User> findByIds(@Param("ids") List<Long> ids) {
        List<User> users = new ArrayList<>();
        List<UserDO> userDOs = userDAO.findByIds(ids);

        for (UserDO uDO : userDOs) {
            users.add(uDO.toModel());
        }

        return users;
    }

    @Override
    public int add(User user) {
        UserDO uDO = UserDOUtil.toDO(user);

        return userDAO.add(uDO);
    }

    @Override
    public int update(User user) {
        UserDO uDO = UserDOUtil.toDO(user);

        return userDAO.update(uDO);
    }

    @Override
    public int delete(@Param("id") long id) {
        return userDAO.delete(id);
    }

    @Override
    public List<User> findAll() {
        List<User> users = new ArrayList<>();
        List<UserDO> userDOs = userDAO.findAll();

        for (UserDO uDO : userDOs) {
            users.add(uDO.toModel());
        }

        return users;
    }

    @Override
    public User findByUserName(@Param("userName") String name) {
        UserDO uDO = userDAO.findByUserName(name);

        return uDO.toModel();
    }

    @Override
    public List<User> query(@Param("keyWord") String keyWord) {
        List<User> users = new ArrayList<>();
        List<UserDO> userDOs = userDAO.query(keyWord);

        for (UserDO uDO : userDOs) {
            users.add(uDO.toModel());
        }

        return users;
    }

    @Override
    public List<User> search(@Param("keyWord") String keyWord, @Param("startTime") LocalDateTime startTime,
                             @Param("endTime") LocalDateTime endTime) {
        List<User> users = new ArrayList<>();
        List<UserDO> userDOs = userDAO.search(keyWord, startTime, endTime);

        for (UserDO uDO : userDOs) {
            users.add(uDO.toModel());
        }

        return users;
    }
}
